


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > ImageSprite</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.sprite</a>
</div>

<h1>Coverage Summary for Class: ImageSprite (nl.tudelft.jpacman.sprite)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ImageSprite</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/15)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.sprite;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.Graphics;
<i class="no-highlight">4</i>&nbsp;import java.awt.GraphicsConfiguration;
<i class="no-highlight">5</i>&nbsp;import java.awt.GraphicsEnvironment;
<i class="no-highlight">6</i>&nbsp;import java.awt.Image;
<i class="no-highlight">7</i>&nbsp;import java.awt.Transparency;
<i class="no-highlight">8</i>&nbsp;import java.awt.image.BufferedImage;
<i class="no-highlight">9</i>&nbsp;
<i class="no-highlight">10</i>&nbsp;/**
<i class="no-highlight">11</i>&nbsp; * Basic implementation of a Sprite, it merely consists of a static image.
<i class="no-highlight">12</i>&nbsp; *
<i class="no-highlight">13</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">14</i>&nbsp; */
<i class="no-highlight">15</i>&nbsp;public class ImageSprite implements Sprite {
<i class="no-highlight">16</i>&nbsp;
<i class="no-highlight">17</i>&nbsp;    /**
<i class="no-highlight">18</i>&nbsp;     * Internal image.
<i class="no-highlight">19</i>&nbsp;     */
<i class="no-highlight">20</i>&nbsp;    private final Image image;
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;    /**
<i class="no-highlight">23</i>&nbsp;     * Creates a new sprite from an image.
<i class="no-highlight">24</i>&nbsp;     *
<i class="no-highlight">25</i>&nbsp;     * @param img
<i class="no-highlight">26</i>&nbsp;     *            The image to create a sprite from.
<i class="no-highlight">27</i>&nbsp;     */
<b class="nc"><i class="no-highlight">28</i>&nbsp;    public ImageSprite(Image img) {</b>
<b class="nc"><i class="no-highlight">29</i>&nbsp;        this.image = img;</b>
<i class="no-highlight">30</i>&nbsp;    }
<i class="no-highlight">31</i>&nbsp;
<i class="no-highlight">32</i>&nbsp;    @Override
<i class="no-highlight">33</i>&nbsp;    public void draw(Graphics graphics, int x, int y, int width, int height) {
<b class="nc"><i class="no-highlight">34</i>&nbsp;        graphics.drawImage(image, x, y, x + width, y + height, 0, 0,</b>
<b class="nc"><i class="no-highlight">35</i>&nbsp;            image.getWidth(null), image.getHeight(null), null);</b>
<i class="no-highlight">36</i>&nbsp;    }
<i class="no-highlight">37</i>&nbsp;
<i class="no-highlight">38</i>&nbsp;    @Override
<i class="no-highlight">39</i>&nbsp;    public Sprite split(int x, int y, int width, int height) {
<b class="nc"><i class="no-highlight">40</i>&nbsp;        if (withinImage(x, y) &amp;&amp; withinImage(x + width - 1, y + height - 1)) {</b>
<b class="nc"><i class="no-highlight">41</i>&nbsp;            BufferedImage newImage = newImage(width, height);</b>
<b class="nc"><i class="no-highlight">42</i>&nbsp;            newImage.createGraphics().drawImage(image, 0, 0, width, height, x,</b>
<i class="no-highlight">43</i>&nbsp;                y, x + width, y + height, null);
<b class="nc"><i class="no-highlight">44</i>&nbsp;            return new ImageSprite(newImage);</b>
<i class="no-highlight">45</i>&nbsp;        }
<b class="nc"><i class="no-highlight">46</i>&nbsp;        return new EmptySprite();</b>
<i class="no-highlight">47</i>&nbsp;    }
<i class="no-highlight">48</i>&nbsp;
<i class="no-highlight">49</i>&nbsp;    private boolean withinImage(int x, int y) {
<b class="nc"><i class="no-highlight">50</i>&nbsp;        return x &lt; image.getWidth(null) &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; image.getHeight(null)</b>
<i class="no-highlight">51</i>&nbsp;            &amp;&amp; y &gt;= 0;
<i class="no-highlight">52</i>&nbsp;    }
<i class="no-highlight">53</i>&nbsp;
<i class="no-highlight">54</i>&nbsp;    /**
<i class="no-highlight">55</i>&nbsp;     * Creates a new, empty image of the given width and height. Its
<i class="no-highlight">56</i>&nbsp;     * transparency will be a bitmask, so no try ARGB image.
<i class="no-highlight">57</i>&nbsp;     *
<i class="no-highlight">58</i>&nbsp;     * @param width
<i class="no-highlight">59</i>&nbsp;     *            The width of the new image.
<i class="no-highlight">60</i>&nbsp;     * @param height
<i class="no-highlight">61</i>&nbsp;     *            The height of the new image.
<i class="no-highlight">62</i>&nbsp;     * @return The new, empty image.
<i class="no-highlight">63</i>&nbsp;     */
<i class="no-highlight">64</i>&nbsp;    private BufferedImage newImage(int width, int height) {
<i class="no-highlight">65</i>&nbsp;        GraphicsConfiguration gc = GraphicsEnvironment
<b class="nc"><i class="no-highlight">66</i>&nbsp;            .getLocalGraphicsEnvironment().getDefaultScreenDevice()</b>
<b class="nc"><i class="no-highlight">67</i>&nbsp;            .getDefaultConfiguration();</b>
<b class="nc"><i class="no-highlight">68</i>&nbsp;        return gc.createCompatibleImage(width, height, Transparency.BITMASK);</b>
<i class="no-highlight">69</i>&nbsp;    }
<i class="no-highlight">70</i>&nbsp;
<i class="no-highlight">71</i>&nbsp;    @Override
<i class="no-highlight">72</i>&nbsp;    public int getWidth() {
<b class="nc"><i class="no-highlight">73</i>&nbsp;        return image.getWidth(null);</b>
<i class="no-highlight">74</i>&nbsp;    }
<i class="no-highlight">75</i>&nbsp;
<i class="no-highlight">76</i>&nbsp;    @Override
<i class="no-highlight">77</i>&nbsp;    public int getHeight() {
<b class="nc"><i class="no-highlight">78</i>&nbsp;        return image.getHeight(null);</b>
<i class="no-highlight">79</i>&nbsp;    }
<i class="no-highlight">80</i>&nbsp;
<i class="no-highlight">81</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
